package com.ycsj.goldmedalnewconcept.activity;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ycsj.goldmedalnewconcept.adapter.MyPagerAdapter;
import com.ycsj.goldmedalnewconcept.bean.GonggaoInfo;
import com.ycsj.goldmedalnewconcept.utils.Bimp;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.R;

//活动预览
public class HuodongPreviewActivity extends Activity implements OnClickListener {

	private ImageView iv_back;
	private Intent intent;
	private int position;
	private String role;
	private String account;
	private String address;
	private String begindate;
	private String enddate;
	private String theme;
	private String content;
	private TextView tv_tag;
	private TextView tv_address;
	private TextView tv_time;
	private TextView tv_content;

	private ViewPager mViewPager;
	private LinearLayout mLinearLayoutDot;
	private List<ImageView> mImageViewList;
	private int currentPosition = 0;
	private List<ImageView> mImageViewDotList;
	private int dotPosition = 0;
	private int prePosition = 0;
	// Bimp.tempSelectBitmap

//	Handler handler = new Handler() {
//		@Override
//		public void handleMessage(Message msg) {
//			super.handleMessage(msg);
//			if (msg.what == 1) {
//				mViewPager.setCurrentItem(currentPosition, false);
//			}
//		}
//	};

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_huodongpreview);
		role = SPUtil.getString(this, "role", "");
		account = SPUtil.getString(this, "account", "");

		intent = getIntent();
		theme = intent.getStringExtra("theme");
		content = intent.getStringExtra("content");
		address = intent.getStringExtra("address");
		begindate = intent.getStringExtra("begindate");
		enddate = intent.getStringExtra("enddate");

		initView();
		setOnListener();
		initData();
		if (Bimp.tempSelectBitmap != null && Bimp.tempSelectBitmap.size() != 0) {
			setDot();
			setViewPager();
//			autoPlay();
		}

		if (Bimp.tempSelectBitmap.size() == 0) {
			ImageView iv = new ImageView(HuodongPreviewActivity.this);
			iv.setBackgroundResource(R.drawable.huodongdefault);
			mImageViewList.add(iv);
			MyPagerAdapter adapter = new MyPagerAdapter(mImageViewList);
			mViewPager.setAdapter(adapter);
		}
	}

	// 设置自动播放
	private void autoPlay() {
		new Thread() {
			@Override
			public void run() {
				super.run();
				while (true) {
					SystemClock.sleep(3000);
					currentPosition++;
//					handler.sendEmptyMessage(1);
				}
			}
		}.start();
	}

	// 设置轮播小圆点
	private void setDot() {
		// 设置LinearLayout的子控件的宽高，这里单位是像素。
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15, 15);
		params.rightMargin = 20;
		// for循环创建images.length个ImageView（小圆点）
		for (int i = 0; i < Bimp.tempSelectBitmap.size(); i++) {
			ImageView imageViewDot = new ImageView(this);
			imageViewDot.setLayoutParams(params);
			// 设置小圆点的背景为暗红图片
			imageViewDot.setBackgroundResource(R.drawable.red_dot_night);
			mLinearLayoutDot.addView(imageViewDot);
			mImageViewDotList.add(imageViewDot);
		}
		// 设置第一个小圆点图片背景为红色
		mImageViewDotList.get(dotPosition).setBackgroundResource(
				R.drawable.red_dot);
	}

	private void setViewPager() {
		MyPagerAdapter adapter = new MyPagerAdapter(mImageViewList);

		mViewPager.setAdapter(adapter);

		mViewPager.setCurrentItem(currentPosition);
		// 页面改变监听
		mViewPager
				.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
					@Override
					public void onPageScrolled(int position,
							float positionOffset, int positionOffsetPixels) {

					}

					@Override
					public void onPageSelected(int position) {
//						if (position == 0) { // 判断当切换到第0个页面时把currentPosition设置为images.length,即倒数第二个位置，小圆点位置为length-1
//							currentPosition = Bimp.tempSelectBitmap.size();
//							dotPosition = Bimp.tempSelectBitmap.size() - 1;
//						} else if (position == Bimp.tempSelectBitmap.size() + 1) { // 当切换到最后一个页面时currentPosition设置为第一个位置，小圆点位置为0
//							currentPosition = 1;
//							dotPosition = 0;
//						} else {
//							currentPosition = position;
//							dotPosition = position - 1;
//						}
//						// 把之前的小圆点设置背景为暗红，当前小圆点设置为红色
//						mImageViewDotList
//								.get(prePosition)
//								.setBackgroundResource(R.drawable.red_dot_night);
//						if (!(dotPosition > mImageViewDotList.size() - 1)) {
//							mImageViewDotList.get(dotPosition)
//									.setBackgroundResource(R.drawable.red_dot);
//							prePosition = dotPosition;
//						}else{
//							mImageViewDotList.get(mImageViewDotList.size() - 1)
//							.setBackgroundResource(R.drawable.red_dot);
//							prePosition = mImageViewDotList.size() - 1;
//						}
						currentPosition = position;
						dotPosition = position;
						mImageViewDotList
						.get(prePosition)
						.setBackgroundResource(R.drawable.red_dot_night);
						
						mImageViewDotList.get(dotPosition)
						.setBackgroundResource(R.drawable.red_dot);
						prePosition = dotPosition;
						
					}

					@Override
					public void onPageScrollStateChanged(int state) {
						// 当state为SCROLL_STATE_IDLE即没有滑动的状态时切换页面
						if (state == ViewPager.SCROLL_STATE_IDLE) {
							mViewPager.setCurrentItem(currentPosition, false);
						}
					}
				});
	}

	private void initView() {
		// TODO Auto-generated method stub
		iv_back = (ImageView) findViewById(R.id.iv_back);

		mViewPager = (ViewPager) findViewById(R.id.vp_main);
		mLinearLayoutDot = (LinearLayout) findViewById(R.id.ll_main_dot);
		tv_tag = (TextView) findViewById(R.id.tv_tag);
		tv_address = (TextView) findViewById(R.id.tv_address);
		tv_time = (TextView) findViewById(R.id.tv_time);
		tv_content = (TextView) findViewById(R.id.tv_content);

	}

	private void initData() {
		tv_tag.setText(theme);
		tv_time.setText(begindate + " 至 " + enddate);
		tv_address.setText(address);
		tv_content.setText(content);
		
		mImageViewList = new ArrayList<ImageView>();
		mImageViewDotList = new ArrayList();
		ImageView imageView = null;
		if (Bimp.tempSelectBitmap != null && Bimp.tempSelectBitmap.size() != 0) {

//			for (int i = 0; i < Bimp.tempSelectBitmap.size() + 2; i++) {
//				if (i == 0) { // 判断当i=0为该处的ImageView设置最后一张图片作为背景
//					imageView = new ImageView(HuodongPreviewActivity.this);
//					imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//					imageView.setImageBitmap(Bimp.tempSelectBitmap.get(
//							Bimp.tempSelectBitmap.size() - 1).getBitmap());
//					mImageViewList.add(imageView);
//				} else if (i == Bimp.tempSelectBitmap.size() + 1) { // 判断当i=images.length+1时为该处的ImageView设置第一张图片作为背景
//					imageView = new ImageView(HuodongPreviewActivity.this);
//					imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//					imageView.setImageBitmap(Bimp.tempSelectBitmap.get(0)
//							.getBitmap());
//					mImageViewList.add(imageView);
//				} else { // 其他情况则为ImageView设置images[i-1]的图片作为背景
//					imageView = new ImageView(HuodongPreviewActivity.this);
//					imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//					imageView.setImageBitmap(Bimp.tempSelectBitmap.get(i - 1)
//							.getBitmap());
//					mImageViewList.add(imageView);
//				}
//			}
			
			for (int i = 0; i < Bimp.tempSelectBitmap.size(); i++) {
					imageView = new ImageView(HuodongPreviewActivity.this);
					imageView.setScaleType(ImageView.ScaleType.FIT_XY);
					imageView.setImageBitmap(Bimp.tempSelectBitmap.get(
							i).getBitmap());
					mImageViewList.add(imageView);
			}

		}

		// tv_title.setText(theme);
		// tv_content.setText(content);

	}

	private void setOnListener() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;
		}
	}

}
